package com.shopx.core.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @ClassName HttpUtils
 * @Description http请求
 * @Author huang_yh
 * @Date 2019.07.24 17:50
 * @Vesion 1.0
 **/
@Slf4j
public class HttpUtils {
    //发起微信支付请求
    public static String httpsRequest(String requestUrl, String requestMethod, String outputStr, String charset) {
        log.info("requestUrl:{},requestMethod:{}, outputStr:{}, charset:{}", requestUrl, requestMethod, outputStr, charset);
        HttpURLConnection conn = null;
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        InputStreamReader inputStreamReader = null;
        try {
            URL url = new URL(requestUrl);
            conn = (HttpURLConnection) url.openConnection();

            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            //conn.setConnectTimeout();
            //conn.setReadTimeout();
            // 设置请求方式（GET/POST）
            conn.setRequestMethod(requestMethod);
            conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes(charset));
                outputStream.close();
            }
            // 从输入流读取返回内容
            inputStream = conn.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, charset);
            bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            log.info("gfsXml:{}", buffer.toString());
            return buffer.toString();
        } catch (ConnectException ce) {
            log.error("连接超时：{}", ce);
            throw new RuntimeException("http.error");
        } catch (Exception e) {
            log.error("https请求异常：{}", e);
            throw new RuntimeException("http.error");
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    log.error("bufferedReader：{}", e);
                    throw new RuntimeException("http.error");
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    log.error("inputStreamReader：{}", e);
                    throw new RuntimeException("http.error");
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                    inputStream = null;
                    conn.disconnect();
                } catch (IOException e) {
                    log.error("inputStream：{}", e);
                    throw new RuntimeException("http.error");
                }
            }
        }
    }
}
